package com.shenbianyun.youfu;

import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.entity.Merchant;
import com.shenbianyun.bosskgpay.utils.*;
import com.shenbianyun.youfu.entity.RequestMsg;
import com.shenbianyun.youfu.entity.SignContract;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

import java.util.HashMap;
import java.util.Map;


public class JmeterTestSignYoufu extends AbstractJavaSamplerClient {


    private String merchant;
    private String PriKey;
    private String InterKey;
    private String url;
    private String charset = "UTF-8";
    private String levy_id;
    /**
     * Holds the result data (shown as Response Data in the Tree display).
     */
    private boolean resultData;
    static Merchant mer = null;

    // 这个方法是用来自定义java方法入参的。
    // params.addArgument("num1","");表示入参名字叫num1，默认值为空。
    //设置可用参数及的默认值；
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("merchant", "89900000040415665332");
        params.addArgument("PriKey", "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIXDDAmxWoqm5Z/9JB6Ivz0tRVRQ3cMn+LVcYGnVZFKcZPou4ZnjzubTLU4/4njbKKrYmhFx1oZCvc+V/2D79XY0f/1L09D+rTGPGCTquEJ1k66qf+VnFUW40WUOJa7WQR7ta1/fFOWn6+J/shqQj6AmA0442eiWcjDvSw8zBT9zAgMBAAECgYBBoPhYDR+KZebGP7KoFJoYDw00GFUmOodpnIBljOCjM77LjZL9KWvLjxNskCWy/HyQjqPRKVSDHV356vOZByZgcvpoobEh0hM2OBc49J6Ne46qRwe2N8ugzcfFml1pmxWqf8kyaer+KMBBv3VyxIRllwYW/9RphbYPwAEhc6Xo8QJBAPVOPulpoVP7nrDEMla9D650bZxcUzL6EGgRhvMbDeVAaxVowMFyRgdzXCXYpNVsnqj4+f1NvzQTfRbzQkmRBxsCQQCLl+hvmc6z6ajhIoG1w/oditSTOvnLMRLUyQtJI1ttGHJjo+03b14AtJDk6Qniuke8n3hvFB/QSfGBgi75hnaJAkAvbXxuZpThhvGsnZJaAN3KTgpvQ852ObD4XgMod5+dJnJNxl4WSMDPafdz/coI1aJraw/ycJi6OdZinI/BZ3/nAkAAu5NkwpHrL9bLF7tGkPjHHutuumiFQISwyT1d7OmV8QLosvuTxunfZxM7oF2E6yVlQBEJxDlGhpzV6TEF7kbBAkAbz8tXcz08OonqYxF6ro2cnE9KpmF1W4nhQcqZZ6L7lbEZJ2+tjYuPGCiGILU11F8KxtgtzwZ+h8RAUilFKTC4");
        params.addArgument("InterKey", "540E40DFD64376E1B1D2CD53D236F200");
        params.addArgument("url", "http://api.youfupingtai.com/pctrans-business/clientBusiness/common.do");
        params.addArgument("levy_id", "35304");

        return params;
    }

    //每个线程测试前执行一次，做一些初始化工作；
    public void setupTest(JavaSamplerContext arg0) {
    }

    //开始测试，从arg0参数可以获得参数值；
    public SampleResult runTest(JavaSamplerContext arg0) {
        SampleResult sr = new SampleResult();
        try {


            sr.sampleStart();// jmeter 开始统计响应时间标记

            merchant = arg0.getParameter("merchant");
            System.out.println(merchant);
            PriKey = arg0.getParameter("PriKey");
            InterKey = arg0.getParameter("InterKey");
            url = arg0.getParameter("url");
            levy_id = arg0.getParameter("levy_id");
            //sr.setSampleLabel( "Java请求哦");

            RequestMsg rm = new RequestMsg();
            rm.setFunCode("6010");
            rm.setMerId(merchant);
            rm.setVersion("V1.0");
            Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();
            rm.setReqId(id+"");

            String json = getJson(Long.parseLong(levy_id));
            byte[] bs = new byte[0];
            try {
                bs = DESUtils.encrypt(json.getBytes(charset), InterKey);
            } catch (Exception e) {

                e.printStackTrace();
            }
            String reqDataEncrypt = Base64Utils.encodeToString(bs);
            rm.setReqData(reqDataEncrypt);
            rm.setSign(RSAUtils.sign(reqDataEncrypt, PriKey));
            String reqStr = JsonUtils.toJson(rm);
            Map<String, Object> map = new HashMap<String, Object>();

            map.put("reqJson", reqStr);
            String resData = HttpClientHelper.httpClientPost(url, map, charset);
            System.out.println(resData);
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            // 通过下面的操作就可以将被测方法的响应输出到Jmeter的察看结果树中的响应数据里面了。
            if (resultData = jsonObject1.get("resCode").equals("0000")) {
                sr.setResponseData("结果是：" + resultData, null);

                //sr.setDataType(SampleResult.TEXT);
                //System.out.println("结果："+resultData);
            } else if (resultData = jsonObject1.get("resCode").equals("6016")) {
                sr.setResponseData("结果是：" + resultData, null);

                //sr.setDataType(SampleResult.TEXT);
                //System.out.println("结果："+resultData);
            }
            sr.setDataType(SampleResult.TEXT);
            sr.setSuccessful(true);
        } catch (Throwable e) {
            System.out.println("异常了");
            sr.setSuccessful(false);
            e.printStackTrace();
        } finally {
            sr.sampleEnd();// jmeter 结束统计响应时间标记
        }
        return sr;
    }

    public static String getJson(Long levyid) {

        SignContract signContract = new SignContract();
        signContract.setName(RandInfo.getRandomJianHan(4));
//      出生年月不要乱填（年龄限制一般为15-65）
        String idcard = new IDCard().getIDCard();

        System.out.println(idcard);
        signContract.setIdCard(idcard);

        //签约方式  1、银行卡（默认）2、支付宝 3、微信
        signContract.setPaymentType(1);

        //银行卡号/支付宝账号/微信openid
        //卡bin前缀 不要乱填
        signContract.setCardNo("6214830102464327");
        signContract.setMobile("18001150364");
        //测试税源地Id
        //signContract.setLevyId(31720L);
        signContract.setLevyId(levyid);
        //签约类型: 0 代表接口签约
        signContract.setSignType(0);

        //*身份证正反面处理，文件不要过大*
        /*String filePathOn = "D:\\工作\\溢美\\身边云\\优付\\测试\\测试数据\\照片\\430381198912267420_p.png";
        String fileStringOn = FileTool.file2Hex(FileTool.getMulFileByPath(filePathOn));
        String filePathBack = "D:\\工作\\溢美\\身边云\\优付\\测试\\测试数据\\照片\\430381198912267420_p.png";
        String fileStringBack = FileTool.file2Hex(FileTool.getMulFileByPath(filePathBack));
        signContract.setIdCardPic1(fileStringOn);
        signContract.setIdCardPic2(fileStringBack);*/


        String json = JsonUtils.toJson(signContract);

        return json;
    }


    //测试结束时调用；
    public void teardownTest(JavaSamplerContext arg0) {
        // System.out.println(end);
        // System.out.println("The cost is"+(end-start)/1000);
    }

     //调试，最后打jar包的时候注释掉。
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Arguments params = new Arguments();
        //params.addArgument("merchant", "89900000040415665332");//设置参数，并赋予默认值1
        //params.addArgument("PriKey", "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIXDDAmxWoqm5Z/9JB6Ivz0tRVRQ3cMn+LVcYGnVZFKcZPou4ZnjzubTLU4/4njbKKrYmhFx1oZCvc+V/2D79XY0f/1L09D+rTGPGCTquEJ1k66qf+VnFUW40WUOJa7WQR7ta1/fFOWn6+J/shqQj6AmA0442eiWcjDvSw8zBT9zAgMBAAECgYBBoPhYDR+KZebGP7KoFJoYDw00GFUmOodpnIBljOCjM77LjZL9KWvLjxNskCWy/HyQjqPRKVSDHV356vOZByZgcvpoobEh0hM2OBc49J6Ne46qRwe2N8ugzcfFml1pmxWqf8kyaer+KMBBv3VyxIRllwYW/9RphbYPwAEhc6Xo8QJBAPVOPulpoVP7nrDEMla9D650bZxcUzL6EGgRhvMbDeVAaxVowMFyRgdzXCXYpNVsnqj4+f1NvzQTfRbzQkmRBxsCQQCLl+hvmc6z6ajhIoG1w/oditSTOvnLMRLUyQtJI1ttGHJjo+03b14AtJDk6Qniuke8n3hvFB/QSfGBgi75hnaJAkAvbXxuZpThhvGsnZJaAN3KTgpvQ852ObD4XgMod5+dJnJNxl4WSMDPafdz/coI1aJraw/ycJi6OdZinI/BZ3/nAkAAu5NkwpHrL9bLF7tGkPjHHutuumiFQISwyT1d7OmV8QLosvuTxunfZxM7oF2E6yVlQBEJxDlGhpzV6TEF7kbBAkAbz8tXcz08OonqYxF6ro2cnE9KpmF1W4nhQcqZZ6L7lbEZJ2+tjYuPGCiGILU11F8KxtgtzwZ+h8RAUilFKTC4");//设置参数，并赋予默认值2
        //params.addArgument("InterKey", "540E40DFD64376E1B1D2CD53D236F200");//设置参数，并赋予默认值2
        //params.addArgument("url", "http://192.168.40.51:8280/pctrans-business/clientBusiness/common.do");//设置参数，并赋予默认值2
        //
        //
        ////http://api.youfupingtai.com/pctrans-business/clientBusiness/common.do
        //
        ////http://192.168.40.51:8280/pctrans-business/clientBusiness/common.do
        //params.addArgument("levy_id", "35304");//设置参数，并赋予默认值2
        //
        //JavaSamplerContext arg0 = new JavaSamplerContext(params);
        //JmeterTestSignYoufu test = new JmeterTestSignYoufu();
        //test.setupTest(arg0);
        //test.runTest(arg0);
        //test.teardownTest(arg0);
    }
}

